package com.aiit.dao;

import com.aiit.pojo.NameValue;
import com.aiit.pojo.order.PurchaseOrder;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface IPurchaseOrderDao extends BaseMapper<PurchaseOrder> {
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,bd_time,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and (state=1 or state=2)")
    List<PurchaseOrder> selectList1();
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,bd_time,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and (state=1 or state=2) ")
    List<PurchaseOrder> selectList2();
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,bd_time,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and state=3 ")
    List<PurchaseOrder> selectList3();
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,bd_time,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and state=1 ")
    List<PurchaseOrder> selectList4();
    @Select("SELECT id,lingjian_id,user_id FROM t_purchase_order WHERE deleted=1 and state=2 ")
    List<PurchaseOrder> selectList5();
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,bd_time,state,bd_time,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and state=2 ")
    List<PurchaseOrder> selectList6();
    @Select("select id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted from t_purchase_order where lingjian_id = #{lingjianId} and merchant_id = #{merchantId} and deleted=1 and (state=1 or state=2)")
    List<PurchaseOrder> selectByLMId(@Param("lingjianId")Integer lingjianId, @Param("merchantId") Integer merchantId);
    @Select("select id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted from t_purchase_order where lingjian_id = #{lingjianId} and merchant_id = #{merchantId} and deleted=1 and state=3 ")
    List<PurchaseOrder> selectByLMId2(@Param("lingjianId")Integer lingjianId, @Param("merchantId") Integer merchantId);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and id=#{parseInt} and (state=1 or state=2)")
    PurchaseOrder selectById2(int parseInt);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and id=#{parseInt} and state=3 ")
    PurchaseOrder selectById3(Integer id);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and id=#{parseInt} and state=1 ")
    PurchaseOrder selectById4(Integer id);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,bd_time,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and id=#{parseInt} and state=2 ")
    PurchaseOrder selectById5(Integer id);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and lingjian_id=#{id} and (state=1 or state=2)")
    List<PurchaseOrder> selectByLId(Integer id);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and lingjian_id=#{id} and state=3")
    List<PurchaseOrder> selectByLId2(Integer id);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,bd_time,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and lingjian_id=#{lingjianId} and state=2")
    List<PurchaseOrder> selectByLId3(Integer lingjianId);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and merchant_id=#{id} and (state=1 or state=2)")
    List<PurchaseOrder> selectByMId(Integer id);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and merchant_id=#{id} and state=3")
    List<PurchaseOrder> selectByMId2(Integer id);
    @Update("update t_purchase_order set state=3 where id=#{id}")
    Integer updateById2(Integer id);
    @Update("update t_purchase_order set true_total_amount=#{trueTotalAmount},update_time=#{updateTime},state=#{state},brim_damage=#{brimDamage},true_number=#{trueNumber},bd_time=#{bdTime}  where id=#{id}")
    boolean updateById3(PurchaseOrder purchaseOrder);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and state=1 and id=#{id} and user_id=#{userId}")
    PurchaseOrder selectByUId(@Param("id")Integer id, @Param("userId")Integer userId);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,bd_time,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and user_id=#{userId} and state=2")
    List<PurchaseOrder> selectByUid2(Integer userId);
    @Select("SELECT id,lingjian_id,number,true_number,total_amount,true_total_amount,user_id,merchant_id,brim_damage,state,bd_time,create_time,deadline,deleted FROM t_purchase_order WHERE deleted=1 and state=2 and id=#{lingjianId} and user_id=#{userId}")
    List<PurchaseOrder> selectByLUId(@Param("lingjianId") Integer lingjianId,@Param("userId") Integer userId);
    @Select("select l.name ,SUM(p.true_number) value from t_lingjian l,t_purchase_order p WHERE p.state=2 and p.deleted=1 and l.deleted=1 and p.lingjian_id=l.id group by lingjian_id ")
    List<NameValue> queryAllSum();
    @Select("select count(*) from t_purchase_order where merchant_id=#{merchantId} and lingjian_id=#{lingjianId} and state=2 and deleted=1")
    Integer queryPOSByMLId(@Param("merchantId") Integer merchantId,@Param("lingjianId") Integer lingjianId);
    @Select("select SUM(true_number) from t_purchase_order where merchant_id=#{merchantId} and lingjian_id=#{lingjianId} and state=2 and deleted=1 ")
    Integer queryPOSByMLId2(@Param("merchantId") Integer merchantId,@Param("lingjianId") Integer lingjianId);
}
